package graph.group.zhangtao.guider.model;

import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.security.PublicKey;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Me on 2017/12/26.
 */

public class ViewNode {
    private String id;
    private String pid;
    private String nid;
    private List<ViewNode> children;
    public ViewNode(String id, String pid, String nid, List<ViewNode> children) {
        this.id = id;
        this.pid = pid;
        this.nid = nid;
        this.children = children;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getNid() {
        return nid;
    }

    public void setNid(String nid) {
        this.nid = nid;
    }

    public List<ViewNode> getChildren() {
        return children;
    }

    public void setChildren(List<ViewNode> children) {
        this.children = children;
    }

    /**
     * 忽略孩子
     * @param json
     * @return
     * @throws JSONException
     */
    public static List<ViewNode> getWithoutChildren(String json) throws JSONException {
        List<ViewNode> result = new LinkedList<>();
        //todo: 孩子处理
        JSONObject jsonObject = new JSONObject(json);
        JSONArray nodes = jsonObject.getJSONArray("nodes");
        for (int i=0;i<nodes.length();i++){
            JSONObject node = nodes.getJSONObject(i);
            ViewNode viewNode = new ViewNode(node.getString("id"),
                    node.getString("preId"),
                    node.getString("nextId"),
                    null
                    );
            result.add(viewNode);
        }
        return result;
    }
    public static ViewNode search(String id, List<ViewNode> nodes){
        for (ViewNode node : nodes) {
            if(node.getId().equals(id)){
                return node;
            }
        }
        return null;
    }
    public static ViewNode getFirst(List<ViewNode> nodes){
        for (ViewNode node : nodes) {
            if(node.getPid().equals("none")){
                return node;
            }
        }
        return null;
    }
}
